package huang.homework11.q2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserConcrol {
		public boolean modifyPwd(User user){
			BaseDao bd=new BaseDao();
			Connection con=bd.getCon();
			String sql1="select pwd,username from user where username='"+user.getUsername()+"'";
			String sql2="update user set pwd=? where username=?";
			try {
				PreparedStatement ps1=con.prepareStatement(sql1);
				ResultSet rs=ps1.executeQuery();
				while(rs.next()){
					//判断输入用户名和原密码是否匹配，如匹配，登陆成功，进行密码修改；
					if(user.getOldpwd().equals(rs.getString("pwd"))&&user.getUsername().equals(rs.getString("username"))){
						System.out.println("登陆成功！");
					}else{
						System.out.println("登陆失败：原密码输入错误");
						return false;
					}
				}
				//初始信息匹配后进行信息的修改；
				PreparedStatement ps2=con.prepareStatement(sql2);
				ps2.setString(1, user.getNewpwd());
				ps2.setString(2, user.getUsername());
				ps2.executeUpdate();
				System.out.println("修改成功！");
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			return true;
		}
}
